#include "mainwindow.h"
#include "./ui_mainwindow.h"


MainWindow* MainWindow::_instance = nullptr;

MainWindow::MainWindow(QWidget *parent)
    : QMainWindow(parent)
    , m_pDefectDetectionDialog(nullptr)
    , ui(new Ui::MainWindow)
{
    ui->setupUi(this);

    _instance = this;

    AllocImageBuffer();
}

MainWindow::~MainWindow()
{
    FreeImageBuffer();
    delete ui;
}

void MainWindow::AllocImageBuffer(){
    int totalImageCount = m_objSeatDetection.GetDetectionWindowInfo().GetImageTotal();
    m_nImageWidth = m_objSeatDetection.GetImageInfo().GetImageWidth();
    m_nImageHeight = m_objSeatDetection.GetImageInfo().GetImageHeight();
    m_nImageChannel = m_objSeatDetection.GetImageInfo().GetImageChannel();

    m_pImageSrcBuffer = (char*)malloc(m_nImageWidth * m_nImageHeight * m_nImageChannel * totalImageCount);
    m_pImageDestBuffer = (char*)malloc(m_nImageWidth * m_nImageHeight * m_nImageChannel * totalImageCount);
}

void MainWindow::FreeImageBuffer(){
    free(m_pImageSrcBuffer);
    free(m_pImageDestBuffer);
}

char* MainWindow::GetImageSrcBuffer(int index){
    char* pBuffer = m_pImageSrcBuffer + m_nImageWidth * m_nImageHeight * m_nImageChannel * index;
    return pBuffer;
}

char* MainWindow::GetImageDestBuffer(int index){
    char* pBuffer = m_pImageDestBuffer + m_nImageWidth * m_nImageHeight * m_nImageChannel * index;
    return pBuffer;
}

MainWindow* MainWindow::instance() {
    return _instance;
}

void MainWindow::on_pushButtonRun_clicked()
{
    if(m_pDefectDetectionDialog == nullptr){
        m_pDefectDetectionDialog = new DefectDetectionDialog(this);
        m_pDefectDetectionDialog->exec();
    }

    delete m_pDefectDetectionDialog;
    m_pDefectDetectionDialog = nullptr;
}

void MainWindow::on_pushButtonTest_clicked()
{

}

void MainWindow::on_pushButtonParameter_clicked()
{
    m_objSeatDetection.ReadXML("D:/test/seat/SeatDetection.xml");
    qDebug() << "SeatDetection.xml";
    //m_objSeatDetection.readJSON("D:/test/seat/SeatDetection.json");
}

